package com.algorithm.learning.base.图;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import com.algorithm.learning.base.图.并查集.UnionSet;

/**
 * @program: algorithm-learning
 * @description:
 * @author: YuKai Fan
 * @create: 2024/8/27 23:51
 **/
public class 并查集_应用 {

    public static class User {
        public final String a;
        public final String b;
        public final String c;

        public User(String a, String b, String c) {
            this.a = a;
            this.b = b;
            this.c = c;
        }
    }

    public static int mergeUsers(List<User> users) {
        UnionSet<User> unionSet = new UnionSet<>(users);

        Map<String, User> mapA = new HashMap<>();
        Map<String, User> mapB = new HashMap<>();
        Map<String, User> mapC = new HashMap<>();

        for (User user : users) {
            if (mapA.containsKey(user.a)) {
                unionSet.union(user, mapA.get(user.a));
            } else {
                mapA.put(user.a, user);
            }
            if (mapB.containsKey(user.b)) {
                unionSet.union(user, mapB.get(user.b));
            } else {
                mapB.put(user.b, user);
            }
            if (mapC.containsKey(user.b)) {
                unionSet.union(user, mapC.get(user.a));
            } else {
                mapC.put(user.b, user);
            }
        }
        // 合并之后，还有多少集合
        return unionSet.getSizeNumber();
    }
}
